package com.hejia.alauda.utils;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import com.alibaba.fastjson.JSONArray;

/**
 * 
 * 爱基金常用工具函数
 *
 *
 * Created by showone on 2015 下午3:45:16.
 *
 * @Copyright: 2015 www.hejiajinrong.com All rights reserved.
 */
public class IFundUtil {
	private static final Log logger = LogFactory.getLog(IFundUtil.class);
	private static final int TIMEOUT = 10000;

	/**
	 * 
	 * @return
	 */
	public static JSONArray fundData() {
		CloseableHttpClient httpclient = HttpClients.createDefault();

		HttpPost httpPost = new HttpPost(
				"http://fund.10jqka.com.cn/public/hjjr/data.txt");
		try {
			RequestConfig config = RequestConfig.custom()
					.setConnectionRequestTimeout(TIMEOUT)
					.setConnectTimeout(TIMEOUT).setSocketTimeout(TIMEOUT)
					.build();
			httpPost.setConfig(config);
			StringEntity se = new StringEntity("", "UTF-8");
			se.setContentType("text/json");
			se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,
					"text/json"));
			httpPost.setEntity(se);
			CloseableHttpResponse response1 = httpclient.execute(httpPost);
			HttpEntity entity1 = response1.getEntity();
			if (entity1 != null) {
				String response = EntityUtils.toString(entity1, "UTF-8");
				JSONArray data = (JSONArray) JSONArray.parse(response);

				return data;
			}
		} catch (Exception e) {
			logger.error("爱基金基金数据获取异常:" + e.toString());
		}
		return null;
	}
}
